<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>销售预测</title>
    <link href="../../component/pear/css/pear.css" rel="stylesheet" />
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">查询月份</label>
                    <div class="layui-input-inline">
                        <input type="text" name="name" placeholder="" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="goods-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="forecast-table" lay-filter="forecast-table"></table>
    </div>
</div>

<script type="text/html" id="role-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
</script>

<script type="text/html" id="role-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
</script>


<script src="../../component/layui/layui.js"></script>
<script src="../../component/pear/pear.js"></script>

<script>
    layui.use(['table','form','jquery','upload'],function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let upload= layui.upload;
        let MODULE_PATH = "./forecast/";
        let cols = [
            [
                {type:'checkbox'},
                {title: 'ID', field: 'forecast_Id', align:'center', width:100},
                {title: '预测标题', field: 'forecast_Title', align:'center', width:100},
                {title: '预测月份', field: 'forecast_Month', align:'center'},
                {title: '预测部门', field: 'dept_Name', align:'center'},
                {title: '创建时间', field: 'createTime', align:'center',templet:function (r){
                        console.log(r.create_Time);
                        return !!r.create_Time?layui.util.toDateString(r.create_Time, 'yyyy-MM-dd HH:mm:ss'):"";
                    }
                },
                {title: '最后操作人', field: 'emp_Name', align:'center'},
                {title: '操作', toolbar: '#role-bar', align:'center', width:195}
            ]
        ]

        table.render({
            elem: '#forecast-table',
            url: 'http://localhost:8181/Manager_war/forecastServlet/list',
            page: true ,
            cols: cols ,
            skin: 'line',
            toolbar: '#role-toolbar',
            headers: {
                token: localStorage.getItem("name")
            },
            defaultToolbar: [{
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        });
        table.on('tool(forecast-table)', function(obj){
            if(obj.event === 'remove'){
                window.remove(obj);
            } else if(obj.event === 'edit'){
                console.log(obj);
                window.edit(obj);//打开 修改的窗体  并且传入这一行的数据
            } else if(obj.event === 'power'){
                window.power(obj);
            }
        });

        table.on('toolbar(forecast-table)', function(obj){
            if(obj.event === 'add'){
                window.add();
            } else if(obj.event === 'refresh'){
                window.refresh();
            } else if(obj.event === 'batchRemove'){
                window.batchRemove(obj);
            }else  if(obj.event==='batchModel'){
                location.href="http://localhost:8181/Manager_war/fileUtilsServlet/batchforecastModel";
            }
        });

        form.on('submit(goods-query)', function(data){
            table.reload('forecast-table',{where:data.field})
            return false;
        });

        window.add = function(){
            layer.open({
                type: 2,
                title: '新增',
                shade: 0.1,
                maxmin: true,
                area: ['800px', '500px'],
                content: MODULE_PATH + 'add.html'
            });
        }


        window.edit = function(obj){
            //存储数据
            localStorage.setItem("forecast_Id",JSON.stringify(obj.data));
            layer.open({
                type: 2,
                title: '修改',
                shade: 0.1,
                area: ['800px', '500px'],
                content: MODULE_PATH +'edit.html?id='+obj.data.forecast_Id
            });
        }

        window.remove = function(obj){
            layer.confirm('确定要删除', {icon: 3, title:'提示'}, function(index){
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url:'http://localhost:8181/Manager_war/forecastServlet/deleteById',
                    data:{
                        "id":obj.data['forecast_Id'],
                    },
                    type:'get',
                    dataType:"json",
                    success:function(result){
                        layer.close(loading);
                        if(result.code==0){
                            layer.msg(result.msg,{icon:1,time:1000},function(){
                                obj.del();
                            });
                        }else{
                            layer.msg(result.msg,{icon:2,time:1000});
                        }
                    }
                })

            });
        }

        window.batchRemove = function(obj){
            let data = table.checkStatus(obj.config.id).data;
            if(data.length === 0){
                layer.msg("未选中数据",{icon:3,time:1000});
                return false;
            }
            let ids = "";
            for(let i = 0;i<data.length;i++){
                ids += data[i].forecast_Id+",";
            }
            ids = ids.substr(0,ids.length-1);
            //ids  =1,2,3,4,5
            layer.confirm('确定要删除', {icon: 3, title:'提示'}, function(index){
                $.ajax({
                    url:'http://localhost:8181/Manager_war/forecastServlet/batchDelete',
                    data:{"ids":ids},
                    dataType: "json",
                    type:'get',
                    success:function(result){
                        if(result.code==0){
                            layer.msg(result.msg,{icon:1,time:1000},function(){
                                table.reload("forecast-table")
                            });
                        }else{
                            layer.msg(result.msg,{icon:2,time:1000});
                        }
                    }
                })



                console.log(ids)

            });
        }

        window.refresh = function(){
            table.reload('forecast-table');
        }
    })
</script>
</body>
</html>